/*
 * Powered By www.cardmee.net
 */
package com.ns.school.controller.wave;

import com.github.pagehelper.PageHelper;
import com.ns.school.common.utils.CommonUtil;
import com.ns.school.common.utils.JsonUtil;
import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.LayuiResult;

import com.ns.school.po.sys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import com.ns.school.controller.base.BaseController;
import com.ns.school.po.wave.WaveStationPO;
import com.ns.school.service.wave.WaveStationService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wt-97
 */
@Slf4j
@Controller
@RequestMapping("station")
public class WaveStationController extends BaseController {

    @Autowired
    private WaveStationService waveStationService;


    @RequestMapping("/list")
    public String list(Model model) {
        return "view/station/list";
    }


    @RequestMapping("/add")
    public String add(Model model, ModelMap modelMap) {
        Map<String, Object> map = new HashMap<>();
        List<User> nameList =waveStationService.selectAllNames(map);
        modelMap.put("userList", nameList);
        return "view/station/add";
    }


    @RequestMapping("/edit")
    public String edit(Model model,Integer id, ModelMap modelMap) {
        WaveStationPO waveStationPO =waveStationService.selecWaveStation(id);// 此处需要通过参数id获取到具体的对象
        modelMap.put("waveStation", waveStationPO);
        return "view/station/edit";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "getWaveStationList")
    @ResponseBody
    public Object findAllStations(WaveStationPO waveStationPO, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        waveStationService.findAll(waveStationPO);
        List<WaveStationPO> listAll = waveStationService.findAllStations(waveStationPO);
        return CommonUtil.getStringObjectMap(listAll);
    }


    //添加
    @RequestMapping("addWaveStation")
    @ResponseBody
    public LayuiResult<WaveStationPO> addStation(@RequestBody WaveStationPO waveStationPO) {
        LayuiResult<WaveStationPO> result = new LayuiResult<>();
        waveStationService.addStation(waveStationPO,result);
        return result;
    }

    //编辑
    @RequestMapping("/updateWaveStation")
    @ResponseBody
    public LayuiResult<WaveStationPO> updateStation(@RequestBody WaveStationPO waveStationPO){
        LayuiResult result = new LayuiResult();
        Integer a =waveStationService.upStation(waveStationPO);
        if(a>0) {
            //修改提示
            result.setCode(0);
            result.setMsg("修改成功！");


        }
        return result;
    }



    /**
     * 删除
     * @param params
     * @return
     */
    @RequestMapping(value = "updateDelWaveStation")
    @ResponseBody
    public LayuiResult<WaveStationPO> updateDel(WaveStationPO params) {
        LayuiResult result = new LayuiResult();
        log.info(JsonUtil.toStr(params));
        waveStationService.updateDel(result,params);
        return result;
    }


    //变电站批量删除
    @RequestMapping("stationUpdate")
    @ResponseBody
    public LayuiResult<WaveStationPO> bookUpdate(WaveStationPO params,String ids) {
        LayuiResult result = new LayuiResult<>();
        String[] strarray = ids.split(",");
        //删除
        for (int i = 0; i < strarray.length; i++) {
            if (!strarray[i].isEmpty()) {
                result.setCode(0);
                result.setMsg("批量删除成功");
                waveStationService.stationUpdate(Integer.valueOf(strarray[i]));
            } else {
                result.setCode(500);
                result.setMsg("删除失败");
            }

        }
        return result;
    }
}
